package info.emamian.droplet.core.domain;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.Transient;
import org.springframework.data.mongodb.core.mapping.Document;

@Document(collection = "sms")
public class BasicSMS {
	public static final String COLLECTION = "sms";
	@Id
	private String id;
	private String from;
	private String to;
	private String body;
	private String provider;
	private Date date;
	Boolean isIncoming;
	private Map<String, String> properties;// anything that is passed by
											// provider

	@Transient
	private List<String> words = null;
	@Transient
	private String firstWord = null;
	@Transient
	private static final String WORD_SPLIT_DELIMITE = " ";

	public BasicSMS(String from, String to, String body, String provider, Date date, Boolean isIncoming, Map<String, String> properties) {
		super();
		this.from = from;
		this.to = to;
		this.body = body.trim();
		this.provider = provider;
		this.date = date;
		this.isIncoming = isIncoming;
		this.properties = properties;
		this.date = date;
	}

	@Override
	public String toString() {
		return String.format("SMS[id=%s, from=%s, to=%s, body=%s, provider=%s, date=%s,incoming=%s, properties=%s,]", id, from, to, body, provider, date, isIncoming, properties);
	}

	public String getBody() {
		return body;
	}

	public void setBody(String body) {
		this.body = body;
	}

	public String getTo() {
		return to;
	}

	public void setTo(String to) {
		this.to = to;
	}

	public String getFrom() {
		return from;
	}

	public void setFrom(String from) {
		this.from = from;
	}

	public Map<String, String> getProperties() {
		return properties;
	}

	public void setProperties(Map<String, String> properties) {
		this.properties = properties;
	}

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public String getProvider() {
		return provider;
	}

	public void setProvider(String provider) {
		this.provider = provider;
	}

	public String getId() {
		return id;
	}

	public Boolean getIsIncoming() {
		return isIncoming;
	}

	public void setIsIncoming(Boolean isIncoming) {
		this.isIncoming = isIncoming;
	}

	public List<String> getWords() {
		if (words == null) {
			String[] tmp = getBody().trim().split(WORD_SPLIT_DELIMITE);
			for (int i = 0; i < tmp.length; i++) {
				tmp[i] = tmp[i].trim();
			}
			words = Arrays.asList(tmp);
		}
		return words;
	}

	public boolean isEmpty() {
		if (body == null || body.isEmpty())
			return true;
		return false;
	}

	public String getFirstWord() {
		if (firstWord == null) {
			int fromIndex = 0;
			if (body.startsWith(WORD_SPLIT_DELIMITE)) {
				fromIndex += WORD_SPLIT_DELIMITE.length();
			}
			int end = body.indexOf(WORD_SPLIT_DELIMITE, fromIndex);
			if (end == -1) {
				end = body.length();
			}
			firstWord = body.substring(fromIndex, end);
		}
		return firstWord;
	}

	public String getBodyMinusFirstWord() {
		return getBody().substring(getFirstWord().length());
	}
}
